package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HmzAssetUsageDetail;
import com.ruoyi.system.domain.HmzProduct;
import com.ruoyi.system.domain.HmzProductType;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HmzAssetUsageMapper;
import com.ruoyi.system.domain.HmzAssetUsage;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用于存储资产领用信息的Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@Service
public class HmzAssetUsageServiceImpl implements IHmzAssetUsageService
{
    @Autowired
    private HmzAssetUsageMapper hmzAssetUsageMapper;
    @Autowired
    private IHmzAssetUsageDetailService hmzAssetUsageDetailService;
    @Autowired
    private IHmzProductTypeService hmzProductTypeService;
    @Autowired
    private IHmzProductService hmzProductService;
    @Autowired
    private ISysUserService userService;



    /**
     * 查询用于存储资产领用信息的
     *
     * @param id 用于存储资产领用信息的主键
     * @return 用于存储资产领用信息的
     */
    @Override
    public HmzAssetUsage selectHmzAssetUsageById(Long id)
    {
        HmzAssetUsage hmzAssetUsage = hmzAssetUsageMapper.selectHmzAssetUsageById(id);
        String createBy = hmzAssetUsage.getCreateBy();
        SysUser sysUser = userService.selectUserById(Long.valueOf(createBy));
        hmzAssetUsage.setCreateByName(sysUser.getUserName());
        HmzAssetUsageDetail hmzAssetUsageDetail = new HmzAssetUsageDetail();
        hmzAssetUsageDetail.setAssetUsageId(id);
        List<HmzAssetUsageDetail> hmzAssetUsageDetails = hmzAssetUsageDetailService.selectHmzAssetUsageDetailList(hmzAssetUsageDetail);
        if(hmzAssetUsageDetails !=null && hmzAssetUsageDetails.size() > 0){
            List<HmzProduct> arrayList = new ArrayList<>();
            for (HmzAssetUsageDetail assetUsageDetail : hmzAssetUsageDetails) {
                Long productId = assetUsageDetail.getProductId();
                HmzProduct hmzProduct = hmzProductService.selectHmzProductById(productId);
                if(hmzProduct !=null){
                    Long productTypeId = hmzProduct.getProductTypeId();
                    if(productTypeId !=null ){
                        HmzProductType hmzProductType = hmzProductTypeService.selectHmzProductTypeById(productTypeId);
                        if(StringUtils.isNotNull(hmzProductType)){
                            hmzProduct.setProductTypeIdName(hmzProductType.getProductTypeName());
                        }
                    }
                }
                arrayList.add(hmzProduct);
            }
            hmzAssetUsage.setInfoListS(arrayList);
        }
        return hmzAssetUsage;
    }

    /**
     * 查询用于存储资产领用信息的列表
     *
     * @param hmzAssetUsage 用于存储资产领用信息的
     * @return 用于存储资产领用信息的
     */
    @Override
    public List<HmzAssetUsage> selectHmzAssetUsageList(HmzAssetUsage hmzAssetUsage)
    {
        List<HmzAssetUsage> hmzAssetUsages = hmzAssetUsageMapper.selectHmzAssetUsageList(hmzAssetUsage);
        if(hmzAssetUsages !=null && hmzAssetUsages.size() > 0){
            for (HmzAssetUsage assetUsage : hmzAssetUsages) {
                String createBy = assetUsage.getCreateBy();
                SysUser sysUser = userService.selectUserById(Long.valueOf(createBy));
                assetUsage.setCreateByName(sysUser.getUserName());
                HmzAssetUsageDetail hmzAssetUsageDetail = new HmzAssetUsageDetail();
                hmzAssetUsageDetail.setAssetUsageId(assetUsage.getId());
                List<HmzAssetUsageDetail> hmzAssetUsageDetails = hmzAssetUsageDetailService.selectHmzAssetUsageDetailList(hmzAssetUsageDetail);
                if(hmzAssetUsageDetails !=null && hmzAssetUsageDetails.size() > 0){
                    List<HmzProduct> arrayList = new ArrayList<>();
                    for (HmzAssetUsageDetail assetUsageDetail : hmzAssetUsageDetails) {
                        Long productId = assetUsageDetail.getProductId();
                        HmzProduct hmzProduct = hmzProductService.selectHmzProductById(productId);
                        if(hmzProduct !=null ){
                            Long productTypeId = hmzProduct.getProductTypeId();
                            if(productTypeId !=null ){
                                HmzProductType hmzProductType = hmzProductTypeService.selectHmzProductTypeById(productTypeId);
                                if(StringUtils.isNotNull(hmzProductType)){
                                    hmzProduct.setProductTypeIdName(hmzProductType.getProductTypeName());
                                }
                            }
                        }

                        arrayList.add(hmzProduct);
                    }
                    assetUsage.setInfoListS(arrayList);
                }
            }
        }
        return hmzAssetUsages;
    }

    /**
     * 新增用于存储资产领用信息的
     *
     * @param hmzAssetUsage 用于存储资产领用信息的
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertHmzAssetUsage(HmzAssetUsage hmzAssetUsage)
    {

        hmzAssetUsage.setCreateTime(DateUtils.getNowDate());
        hmzAssetUsage.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
//        hmzAssetUsage.setTotalQuantity((long) hmzAssetUsage.getInfoList().size());
        int i = hmzAssetUsageMapper.insertHmzAssetUsage(hmzAssetUsage);
        Long id = hmzAssetUsage.getId();
        String assetUsageNumber = hmzAssetUsage.getAssetUsageNumber();
        List<HmzAssetUsageDetail> infoList = hmzAssetUsage.getInfoList();
        if(infoList !=null && infoList.size() > 0){
            for (HmzAssetUsageDetail hmzAssetUsageDetail : infoList) {
                hmzAssetUsageDetail.setAssetUsageId(id);
                hmzAssetUsageDetail.setAssetUsageNumber(assetUsageNumber);
                Long productId = hmzAssetUsageDetail.getId();
                HmzProduct hmzProduct = new HmzProduct();
                hmzProduct.setId(productId);
                hmzProduct.setProductStatus(hmzAssetUsageDetail.getUsageStatus());
                //新增出库单不更新产品
//                hmzProductService.updateHmzProduct(hmzProduct);
//                hmzAssetUsageDetail.setProductId(hmzAssetUsageDetail.getId());
                hmzAssetUsageDetail.setProductId(hmzAssetUsageDetail.getId());
                hmzAssetUsageDetail.setUserName(String.valueOf(SecurityUtils.getUserId()));
                hmzAssetUsageDetailService.insertHmzAssetUsageDetail(hmzAssetUsageDetail);
            }

        }

        return i;
    }

    /**
     * 修改用于存储资产领用信息的
     *
     * @param hmzAssetUsage 用于存储资产领用信息的
     * @return 结果
     */
    @Override
    public int updateHmzAssetUsage(HmzAssetUsage hmzAssetUsage)
    {
        hmzAssetUsage.setUpdateTime(DateUtils.getNowDate());
        List<HmzAssetUsageDetail> infoList = hmzAssetUsage.getInfoList();
        return hmzAssetUsageMapper.updateHmzAssetUsage(hmzAssetUsage);
    }

    /**
     * 批量删除用于存储资产领用信息的
     *
     * @param ids 需要删除的用于存储资产领用信息的主键
     * @return 结果
     */
    @Override
    public int deleteHmzAssetUsageByIds(Long[] ids)
    {
        return hmzAssetUsageMapper.deleteHmzAssetUsageByIds(ids);
    }

    /**
     * 删除用于存储资产领用信息的信息
     *
     * @param id 用于存储资产领用信息的主键
     * @return 结果
     */
    @Override
    public int deleteHmzAssetUsageById(Long id)
    {
        return hmzAssetUsageMapper.deleteHmzAssetUsageById(id);
    }
}
